//
//  MGYShareSheetView.m
//  EvansTool
//
//  Created by XiaoMing on 16/3/3.
//  Copyright © 2016年 Evans Music. All rights reserved.
//

#import "MGYShareSheetView.h"

#define IPHONE_HEIGHT  [UIScreen mainScreen].bounds.size.height
#define IPHONE_WIDTH   [UIScreen mainScreen].bounds.size.width

@implementation MGYShareSheetView{
    UIView      *_backView;
    UIView      *_actionSheetView;
    CGFloat     _actionSheetHeight;
    CGFloat     _buttonHeight;
    BOOL        _isShow;
    NSArray * _nameArr;
    NSArray * _imageArr;
}

- (void)dealloc {
    _actionSheetView            = nil;
    _backView                   = nil;
    _selectRowBlock             = nil;
}

- (instancetype)init {
    self = [super init];
    if (self){
        CGRect frame = [UIScreen mainScreen].bounds;
        self.frame = frame;
        _buttonHeight = (IPHONE_WIDTH-175)/4;
        _nameArr = @[@"微信好友",@"朋友圈",@"QQ好友",@"QQ空间"];
        _imageArr = @[@"weixinfriend",@"weixinzone",@"qqfriend",@"qqzone"];
    }
    return self;
}

- (instancetype)initWithSelectButtonBlock:(MGYShareSheetViewDidSelectButtonBlock)block{
    self = [self init];
    
    if (self) {
        _selectRowBlock = block;
        
        _backView = [[UIView alloc] initWithFrame:self.frame];
        _backView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
        _backView.alpha = 0.0f;
        [self addSubview:_backView];
        
        _actionSheetView = [[UIView alloc] init];
        _actionSheetView.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:_actionSheetView];
        
        for (int i = 0; i < 4; i ++) {
            UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(35+i*(_buttonHeight+35), 20, _buttonHeight, _buttonHeight)];
            [_actionSheetView addSubview:button];
            button.tag = i;
            [button setBackgroundImage:[UIImage imageNamed:[_imageArr objectAtIndex:i]] forState:0];
            [button addTarget:self action:@selector(didSelectAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        for (int i = 0; i < 4; i ++) {
            UILabel * titleLable = [[UILabel alloc]initWithFrame:CGRectMake(20+i*(_buttonHeight+35), 25+_buttonHeight, _buttonHeight+30, 20)];
            titleLable.text = [_nameArr objectAtIndex:i];
            titleLable.textAlignment = NSTextAlignmentCenter;
            titleLable.font = [UIFont systemFontOfSize:13];
            [_actionSheetView addSubview:titleLable];
        }
        
        UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 55+_buttonHeight, IPHONE_WIDTH, 5)];
        lineView.backgroundColor = [UIColor lightGrayColor];
        lineView.alpha = 0.3;
        [_actionSheetView addSubview:lineView];
        
        UIButton * cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 60+_buttonHeight, IPHONE_WIDTH, 45)];
        [cancelButton setTitle:@"取 消" forState:0];
        cancelButton .titleLabel.font = [UIFont systemFontOfSize:16];
        [cancelButton setTitleColor:[UIColor blackColor] forState:0];
        [_actionSheetView addSubview:cancelButton];
        [cancelButton addTarget:self action:@selector(clickCancleButton) forControlEvents:UIControlEventTouchUpInside];
        
        _actionSheetHeight = 105+_buttonHeight;
        
        _actionSheetView.frame = CGRectMake(3, CGRectGetHeight(self.frame), CGRectGetWidth(self.frame)-6, _actionSheetHeight);
        _actionSheetView.layer.cornerRadius = 5.0f;
        _actionSheetView.layer.masksToBounds = YES;
    }
    
    return self;
}

-(void)clickCancleButton{
    [self dismiss];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:_backView];
    if (!CGRectContainsPoint([_actionSheetView frame], point)) {
        [self dismiss];
    }
}

- (void)didSelectAction:(UIButton *)button {
    if (_selectRowBlock){
        NSInteger index = button.tag;
        _selectRowBlock(index);
    }
    [self dismiss];
}


#pragma mark - public
- (void)show {
    if(_isShow) return;
    _isShow = YES;
    [UIView animateWithDuration:0.35f delay:0 usingSpringWithDamping:0.9f initialSpringVelocity:0.7f options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionLayoutSubviews animations:^{
        
        [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
        _backView.alpha = 1.0;
        _actionSheetView.frame = CGRectMake(2, CGRectGetHeight(self.frame)-_actionSheetHeight, CGRectGetWidth(self.frame)-4, _actionSheetHeight);
    } completion:NULL];
}

- (void)dismiss
{
    _isShow = NO;
    [UIView animateWithDuration:0.35f delay:0 usingSpringWithDamping:0.9f initialSpringVelocity:0.7f options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionLayoutSubviews animations:^{
        _backView.alpha = 0.0;
        _actionSheetView.frame = CGRectMake(0, CGRectGetHeight(self.frame), CGRectGetWidth(self.frame), _actionSheetHeight);
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
